﻿using System;

namespace cc.hanzs.json
{
    public class JSONPath {
        private String[] path_List = null;
        private JSONPath d副本_JSONPath = new JSONPath();
        public System.Object root, i当前;

        private JSONPath() { }

        public JSONPath d副本(String cpath_s) {
            JSONPath jd = (JSONPath)d副本_JSONPath.MemberwiseClone();

            String[] jipath_ss;

            jipath_ss = cpath_s.Split(new char[1] { '/'});//用分隔符“/”将路径分割成文本组

            //将文本组添加到集合，首个“.”添加到集合，其余“.”不添加到集合
            int ji_i = 0;
            for (int jji_i = 0; jji_i < jipath_ss.Length; jji_i++){
                if ((jji_i == 0 || !jipath_ss[jji_i].Equals(".")) && !jipath_ss[jji_i].Equals("")){
                    ji_i++;
                }
            }
            jd.path_List = new String[ji_i];
            ji_i = 0;
            for (int jji_i = 0; jji_i < jipath_ss.Length; jji_i++) {
                if ((jji_i == 0 || !jipath_ss[jji_i].Equals(".")) && !jipath_ss[jji_i].Equals("")) {
                    jd.path_List[ji_i] = jipath_ss[jji_i];
                    ji_i++;
                }
            }

            //清理path_List内的“..”   jg清理path_List内的上级路径();
            bool ji被清理_b;
            do {
                ji被清理_b = false;
                ji_i = jd.path_List.Length;
                for (int jji_i = jd.path_List.Length - 1; jji_i > 0; jji_i--) {
                    String jji_s = jd.path_List[jji_i];
                    if (!jji_s.Equals("..")) {
                        continue;
                    }
                    String jji1_s = jd.path_List[jji_i - 1];
                    if (jji1_s.Equals(".")) {
                        jd.path_List[jji_i - 1] = null;
                        ji_i--;
                        ji被清理_b = true;
                    } else if (jji1_s.Equals("..")) {
                    } else {
                        jd.path_List[jji_i] = null;
                        jd.path_List[jji_i - 1] = null;
                        ji_i--;
                        ji_i--;
                        ji被清理_b = true;
                    }
                }
                if (ji被清理_b) {
                    ji_i = 0;
                    for (int jji_i = 0; jji_i < jd.path_List.Length; jji_i++) {
                        if (jd.path_List[jji_i] != null){
                            jd.path_List[ji_i] = jd.path_List[jji_i];
                            ji_i++;
                        }
                    }
                    String[] jpath_List = new String[ji_i];
                    System.Array.Copy(jd.path_List, jpath_List, ji_i);
                    jd.path_List = jpath_List;
                }
            } while (ji被清理_b);

            return jd;
        }

        public System.Object get() {
            System.Object ji1;

            String ji_s = path_List[0];
            if (ji_s.Equals(".") || ji_s.Equals("..")) {
                ji1 = i当前;
            } else {
                ji1 = root;
            }

            for (int jji_i = 0; jji_i < path_List.Length; jji_i++) {
                String jji_s = path_List[jji_i];
                if (ji1 is JSONObject) {
                    if (jji_s.Equals(".")) {
                    } else if (jji_s.Equals("..")) {
                        ji1 = ((JSONObject) ji1).parent;
                    } else {
                        ji1 = ((JSONObject) ji1).get(jji_s);
                    }
                } else if (ji1 is JSONArray) {
                    if (jji_s.Equals(".")) {
                    } else if (jji_s.Equals("..")) {
                        ji1 = ((JSONArray) ji1).parent;
                    } else {
                        ji1 = ((JSONArray)ji1).get(System.Convert.ToInt32(jji_s));
                    }
                } else {
                    break;
                }
            }
            return ji1;
        }

        /**
         *
         * @return 序列化後的文本
         */
        public String toString() {
            System.Text.StringBuilder ji_S = new System.Text.StringBuilder(path_List.Length * 32);

            for (int jji_i = 0; jji_i < path_List.Length; jji_i++) {
                String jji_s = path_List[jji_i];
                ji_S.Append(jji_s);
                ji_S.Append("/");
            }
            int ji_i = ji_S.Length;
            if (ji_i > 0) {
                ji_S.Remove(ji_i - 1, 1);
            }
            String ji_s = ji_S.ToString();
            if (!ji_s.Equals(".") && !ji_s.Equals("..") && !ji_s.StartsWith("../") && !ji_s.StartsWith("./")) {
                ji_s = "/" + ji_s;
            }
            return ji_s;
        }
    }
}
